<?php

namespace App\Http\Controllers\Web\Seller;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;
use App\Repository\Repositories\Interfaces\ReceivingAccountRepository;
use App\Repository\Repositories\Interfaces\AttributeValuesRepository;

 
class AttributeValueController extends BaseController
{
   
    protected $receiving_account_repo;
    protected $shop_id;
    protected $user_id;
    
    public function __construct(AttributeValuesRepository $attribute_value_repo)
    {
        parent::__construct();
        $this->attribute_value_repo = $attribute_value_repo;
        //用户只能操作自身所属数据
  
        $this->shop_id =isset($this->user) &&  isset($this->user->shop_id) ? $this->user->shop_id : 0;
        
        $this->attribute_value_repo->scopeQuery(function ($query) {
            return $query->where(['shop_id'=> $this->shop_id]);
        });
    }

    public function index()
    {
        $orders = $this->attribute_value_repo->paginate($this->perPage);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $orders, true);
    }

    public function show($id)
    {
        $product = $this->attribute_value_repo->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $product['data']);
    }
  

    public function store(Request $request)
    { 
        $this->validate($request,[
            'value'=> 'required|string',
            'attribute_id'=> 'required|integer|exists:attributes,id',
            'sort'=>'integer',
        ]);
        
        $data = $request->all();
        
        $ret = $this->attribute_value_repo->create($data);
        if(isset($ret['data'])){
            return $this->success(ErrorConst::SUCCESS_CODE, $ret['data']);
        }else{
            return $this->error(ErrorConst::FAILED_CODE);
        }
    }

    public function update(Request $request, $id)
    { 
        $this->validate($request,[
            'value'=> 'required|string',
            'attribute_id'=> 'required|integer|exists:attributes,id',
            'sort'=>'integer',
        ]);
        $data = $request->all();
        
        $ret = $this->attribute_value_repo->update($data, $id);
        if($ret){
            return $this->success(ErrorConst::SUCCESS_CODE, ErrorConst::SUCCESS_CODE_MSG);
        }else{
            return $this->error(ErrorConst::FAILED_CODE);
        }
    }

    public function delete(Request $request, $id)
    {
        $ret = $this->attribute_value_repo->delete($id);
        return $this->noDataReturn($ret ? true : false);
    }
    
}
